package com.lxy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 请不要模仿我
 *
 * @author lxy
 * @date 2022/9/30 9:12
 * com.lxy.login.interceptor.
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截器的前置拦截
        System.out.println("前置拦截");
        //满足条件返回true 不满足返回false
//        return HandlerInterceptor.super.preHandle(request, response, handler);
        return true;
    }

/*
* 后置拦截 是controller 响应之后在次经过拦截器时会经过后置拦截
* 是在视图解析器 解析试图之前 执行的    也就是可以在解析试图
* */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("后置拦截");
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
    /*
    * 在页面渲染之后执行 整个请求都执行结束了 这个方法才执行
    * 可以在这个方法中做一些收尾工作 比如日志 资源清理*/
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("拦截后渲染");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
